24-4 qt庥瑊z

我們可以列出與 SYSTEM 相關的重要環境變數:

原始檔(envVarSystem01.js):(灰色區域按兩下即可拷貝)
// 列出重要的環境變數

wshShell = WScript.CreateObject("WScript.Shell");
wshSysEnv = wshShell.Environment("SYSTEM");
WScript.Echo("重要的環境變數:");
name="windir"; WScript.Echo(name+" = "+wshSysEnv(name));
name="path"; WScript.Echo(name+" = "+wshSysEnv(name));
name="NUMBER_OF_PROCESSORS"; WScript.Echo(name+" = "+wshSysEnv(name));
name="OS"; WScript.Echo(name+" = "+wshSysEnv(name));
name="PROCESSOR_ARCHITECTURE"; WScript.Echo(name+" = "+wshSysEnv(name));
name="temp"; WScript.Echo(name+" = "+wshSysEnv(name));

典型顯示結果如下:

重要的環境變數: windir = %SystemRoot% path = %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;c:\matlab6p 5\bin\win32;C:\Program Files\Microsoft SQL Server\90\Tools\binn\ NUMBER_OF_PROCESSORS = 1 OS = Windows_NT PROCESSOR_ARCHITECTURE = x86 temp = %SystemRoot%\TEMP 我們可以列出與 SYSTEM 相關的全部環境變數:

原始檔(envVarSystem02.js):(灰色區域按兩下即可拷貝)
// 所有的 SYSTEM 環境變數列表

shell = WScript.CreateObject("WScript.Shell");
envObj = shell.Environment("SYSTEM");
WScript.Echo("所有的 SYSTEM 環境變數列表:");
WScript.Echo("No. of env. variables = "+envObj.length);
var Enum=new Enumerator(envObj) 
for (Enum.moveFirst(); !Enum.atEnd(); Enum.moveNext())
	WScript.Echo(Enum.item());

典型顯示結果如下:

所有的 SYSTEM 環境變數列表: No. of env. variables = 12 ComSpec=%SystemRoot%\system32\cmd.exe Path=C:\PROGRAM FILES\THINKPAD\UTILITIES;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:\Program Files\PC-Doctor for Windows\services;c:\matlab6p5\bin\win32 windir=%SystemRoot% OS=Windows_NT PROCESSOR_ARCHITECTURE=x86 PROCESSOR_LEVEL=6 PROCESSOR_IDENTIFIER=x86 Family 6 Model 9 Stepping 5, GenuineIntel PROCESSOR_REVISION=0905 NUMBER_OF_PROCESSORS=1 PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH TEMP=%SystemRoot%\TEMP TMP=%SystemRoot%\TEMP 我們可以列出與 PROCESS 相關的全部環境變數:

原始檔(envVarProcess01.js):(灰色區域按兩下即可拷貝)
// 所有的 PROCESS 環境變數列表

shell = WScript.CreateObject("WScript.Shell");
envObj = shell.Environment("PROCESS");
WScript.Echo("所有的 PROCESS 環境變數列表:");
WScript.Echo("No. of env. variables = "+envObj.length);
var Enum=new Enumerator(envObj) 
for (Enum.moveFirst(); !Enum.atEnd(); Enum.moveNext())
	WScript.Echo(Enum.item());

典型顯示結果如下:

所有的 PROCESS 環境變數列表: No. of env. variables = 35 =::=::\ =C:=C:\Documents and Settings\user =D:=D:\users\jang\books\wsh\example =ExitCode=00000000 ALLUSERSPROFILE=C:\Documents and Settings\All Users APPDATA=C:\Documents and Settings\user\Application Data CLASSPATH=.;.;C:\PROGRA~1\JMF21~1.1E\lib\sound.jar;C:\PROGRA~1\JMF21~1.1E\lib\jmf.jar;C:\PROGRA~1\JMF21~1.1E\lib;%systemroot%\java\classes;. CommonProgramFiles=C:\Program Files\Common Files COMPUTERNAME=ROGER ComSpec=C:\WINDOWS\system32\cmd.exe HOMEDRIVE=C: HOMEPATH=\Documents and Settings\user include=C:\Program Files\Microsoft Visual Studio\VC98\atl\include;C:\Program Files\Microsoft Visual Studio\VC98\mfc\include;C:\Program Files\Microsoft Visual Studio\VC98\include lib=C:\Program Files\Microsoft Visual Studio\VC98\mfc\lib;C:\Program Files\Micro soft Visual Studio\VC98\lib LOGONSERVER=\\ROGER MSDevDir=C:\Program Files\Microsoft Visual Studio\Common\MSDev98 NUMBER_OF_PROCESSORS=1 OS=Windows_NT Path=C:\PROGRAM FILES\THINKPAD\UTILITIES;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATITechnologies\ATI Control Panel;C:\Program Files\PC-Doctor for Windows\services;c:\matlab6p5\bin\win32;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin;;c:\borland\Bcc55\bin;d:\users\jang\batch;d:\users\jang\util PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH PROCESSOR_ARCHITECTURE=x86 PROCESSOR_IDENTIFIER=x86 Family 6 Model 9 Stepping 5, GenuineIntel PROCESSOR_LEVEL=6 PROCESSOR_REVISION=0905 ProgramFiles=C:\Program Files PROMPT=$P$G SESSIONNAME=Console SystemDrive=C: SystemRoot=C:\WINDOWS TEMP=C:\DOCUME~1\user\LOCALS~1\Temp TMP=C:\DOCUME~1\user\LOCALS~1\Temp USERDOMAIN=ROGER USERNAME=Roger USERPROFILE=C:\Documents and Settings\user windir=C:\WINDOWS 我們可以使用 WSH 來改變與系統相關的設定,例如,我們可以使用 RegRead() 來讀出 Registry 的值,例如: Error: D:\users\jang\books\wsh/example/registry01.js does not exist!